package com.study.system.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author 669
 * @CreateDate 2022/4/6 8:55
 * @Version 1.0
 */
public class MybatisUtils {

    public static SqlSession getSqlSession() {

        SqlSession sqlSession = null;
        try {
            //读取配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");
            //构建session工厂实例
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            sqlSession = build.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }


    public static SqlSession getUserSqlSession(EmpUse userMybatis) {
        SqlSession sqlSession = null;
        try {
            //读取配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");
            //构建session工厂实例
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            //获取连接对象
            sqlSession = build.openSession(true);
            //使用use方法
            userMybatis.use(sqlSession);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }

        return sqlSession;
    }


}



